home *** CD-ROM | disk | FTP | other *** search
/ SGI Performance Co-Pilot 1.3 / SGI Performance Co-Pilot 1.3.iso / dist / pcp.idb / usr / pcp / lib / pmie-common.z / pmie-common
Text File  |  1997-04-03  |  4KB  |  156 lines

  1. #!/bin/sh
  2. #
  3. #  WARNING: BEGIN
  4. #    DO NOT MODIFY THIS SCRIPT.  YOUR CHANGES MAY BE LOST!
  5. #
  6. #  This script was installed as part of the Performance Co-Pilot
  7. #  software installation.
  8. #
  9. #  WARNING: END
  10. #
  11. # Copyright 1995, Silicon Graphics, Inc.
  12. # ALL RIGHTS RESERVED
  13. # UNPUBLISHED -- Rights reserved under the copyright laws of the United
  14. # States.   Use of a copyright notice is precautionary only and does not
  15. # imply publication or disclosure.
  16. # U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  17. # Use, duplication or disclosure by the Government is subject to restrictions
  18. # as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  19. # in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  20. # in similar or successor clauses in the FAR, or the DOD or NASA FAR
  21. # Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  22. # 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  23. # THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  24. # INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  25. # DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  26. # PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  27. # GRAPHICS, INC.
  28. #
  29. # common /bin/sh procedures for standard pmie action scripts
  30. #
  31. # $Id: common.sh,v 2.1 1997/03/26 03:09:56 kenmcd Exp $
  32.  
  33. PMIEDIR=/tmp/pmie    # control and lock files live here, directory is
  34.             # made writeable by everyone
  35. LOCK=$PMIEDIR/lock    # lock file
  36. LOCKWAIT=5        # wait this long for the lock, then give up
  37.  
  38. # control files ... copy $MASTER to $CONTROL if required, and make
  39. # $CONTROL writeable by everyone
  40. MASTER=/var/pcp/config/pmie/control.master
  41. CONTROL=$PMIEDIR/control
  42.  
  43. # __init
  44. # initialize the temporary directory
  45. __init()
  46. {
  47.     if [ ! -d $PMIEDIR ]
  48.     then
  49.     mkdir $PMIEDIR
  50.     chmod 777 $PMIEDIR
  51.     fi
  52.     if [ ! -f $CONTROL ]
  53.     then
  54.     cp $MASTER $CONTROL
  55.     chmod 666 $CONTROL
  56.     fi
  57. }
  58.  
  59. # _lock
  60. # return 0 if lock acquired, else 1
  61. #
  62. _lock()
  63. {
  64.     __init
  65.     i=0
  66.     while [ $i -lt $LOCKWAIT ]
  67.     do
  68.     if /usr/pcp/bin/pmlock $LOCK
  69.     then
  70.         return 0
  71.     fi
  72.     sleep 1
  73.     i=`expr $i + 1`
  74.     done
  75.  
  76.     return 1
  77. }
  78.  
  79. # _unlock
  80. # unilaterally unlock, be removing the file
  81. #
  82. _unlock()
  83. {
  84.     rm -f $LOCK
  85. }
  86.  
  87. # _ok action-name
  88. # check if action may proceed
  89. #
  90. _ok()
  91. {
  92.     __init
  93.     mintime=""
  94.     eval `nawk <$CONTROL '
  95. $1 == "'"$1"'"    { print "mintime=" $2 " maxconcur=" $3 " laststamp=" $4 }'`
  96.     # if no entry in the control file, then no restrictions
  97.     [ "X$mintime" = "X" ] && return 0
  98.     if [ "X$mintime" != X- ]
  99.     then
  100.     # minimum time between invocations specified
  101.     check=`pmdate -${mintime}S %y%m%d%H%M%S`
  102.     # paranoia, in case file may have been corrupted
  103.     [ "X$laststamp" = X ] && laststamp=000000000000
  104.     if [ "$check" -lt "$laststamp" ]
  105.     then
  106.         # too soon
  107.         return 1
  108.     fi
  109.     fi
  110.     if [ "X$maxconcur" != X- ]
  111.     then
  112.     # maximum number of concurrent invocations specified
  113.     concur=`ps -ef \
  114.         | sed -e 's/ [0-2][0-9]:[0-5][0-9]:[0-5][0-9] / To Day /' \
  115.         | nawk '
  116. $9 ~ /\/'"$1"'$/ || $9 == "'"$1"'"            { print; next }
  117. $9 ~ /sh$/ && ( $10 ~ /\/'"$1"'$/ || $10 == "'"$1"'" )    { print; next }
  118. $9 ~ /sh$/ && $10 ~ /^-/ \
  119. && ( $11 ~ /\/'"$1"'$/ || $11 == "'"$1"'" )        { print; next }' \
  120.         | wc -l`
  121.     if [ "$concur" -gt "$maxconcur" ]
  122.     then
  123.         # too many
  124.         return 1
  125.     fi
  126.     fi
  127.  
  128.     return 0
  129. }
  130.  
  131. # _stamp action-name
  132. # update date and time stamp, if required
  133. #
  134. _stamp()
  135. {
  136.     __init
  137.     mintime=""
  138.     eval `nawk <$CONTROL '
  139. $1 == "'"$1"'"    { print "mintime=" $2 " maxconcur=" $3 " laststamp=" $4 }'`
  140.     # if no entry in the control file, then nothing to update
  141.     [ "X$mintime" = "X" ] && return
  142.     if [ "X$mintime" != X- ]
  143.     then
  144.     # minimum time between invocations specified, so update
  145.     now=`pmdate +0S %y%m%d%H%M%S`
  146.     nawk <$CONTROL >$CONTROL.new '
  147. $1 == "'"$1"'"    { print $1 "    " $2 "    " $3 "    '"$now"'"; next }
  148.         { print }'
  149.     mv $CONTROL.new $CONTROL
  150.     chmod 666 $CONTROL
  151.     fi
  152. }
  153.